package com.yaozheng.vocationaltraining.db;

import com.yaozheng.vocationaltraining.activity.BaseActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DbOpenHelper extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 2;
	private static DbOpenHelper instance;

	private DbOpenHelper(BaseActivity baseActivity) {
		super(baseActivity, getUserDatabaseName(baseActivity), null,
				DATABASE_VERSION);
	}

	public DbOpenHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public synchronized static DbOpenHelper getInstance(
			BaseActivity baseActivity) {
		if (instance == null) {
			instance = new DbOpenHelper(baseActivity);
		}
		return instance;
	}

	/**
	 * 清空
	 */
	public synchronized static void clearDbOpenHelper() {
		instance = null;
	}

	private static String getUserDatabaseName(BaseActivity baseActivity) {
		return baseActivity.getToken() + ".db";
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createTable(db);

	}

	public void createTable(SQLiteDatabase db) {
		db.execSQL(JSONDBCacheDao.CREATE_TABLE_SQL);
		db.execSQL(CoursewareCacheDao.CREATE_TABLE_SQL);
		db.execSQL(PaperAnswerCacheDao.CREATE_TABLE_SQL);
		// db.execSQL(UserDao.CREATE_TABLE_SQL);
		// db.execSQL(INIVTE_MESSAGE_TABLE_CREATE);
		// db.execSQL(OrderDataCacheDao.CREATE_TABLE_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		createTable(db);
	}

}
